using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ExtendedMvc.Common.Diagnostics;
using ExtendedMvc.Html;

namespace ExtendedMvc.Elements {
	public class OrderedList : ListBase<OrderedList> {
		public OrderedList(string id = null) : base(HtmlListType.OrderedList, id) { }
	}

	public class UnorderedList : ListBase<UnorderedList> {
		public UnorderedList(string id = null) : base(HtmlListType.UnorderedList, id) { }
	}


	public abstract class ListBase<T> : VisibleElement<T> where T : ListBase<T> {
		private IEnumerable<ListItem> ListItems;

		protected ListBase(string listType, string id = null)
			: base(listType, id) {
		}

		public virtual T AddItem(string content) {
			Check.Argument.IsNotNull(content, "content");
			if (ListItems == null) ListItems = new List<ListItem>();
			(ListItems as List<ListItem>).Add(new ListItem(content));
			return (T)this;
		}

		public virtual T AddItem(ListItem item) {
			Check.Argument.IsNotNull(item, "item");
			if (ListItems == null) ListItems = new List<ListItem>();
			(ListItems as List<ListItem>).Add(item);
			return (T)this;
		}

		public virtual T Items(IEnumerable<string> contents) {
			Check.Argument.IsNotNull(contents, "contents");
			ListItems = new List<ListItem>();
			foreach (var value in contents) AddItem(value);
			return (T)this;
		}

		public virtual T Items(IEnumerable<ListItem> items) {
			ListItems = items;
			return (T)this;
		}

		protected override TagRenderMode TagRenderMode { get { return TagRenderMode.Normal; } }

		protected override void SetContent() {
			if (ListItems == null || !ListItems.Any()) return;
			foreach (var item in ListItems)
				Builder.InnerHtml += item.ToString();
		}
	}
}